const Tools = {
    /**************************  正则表达式  **************************/
    isPhone:function(val){
        if(!(/^1[34578]\d{9}$/.test(val))){
            return false
        }
        return true
    },
    isIdCard:function(val){
        if(!(/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/.test(val))&&!(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/.test(val))){
            return false
        }
        return true
    },
    /**************************  正则表达式  **************************/
    /**************************  日期时间类型工具函数  **************************/
    /*@param date 时间戳*/
    /*@param format 时间格式  'yyyy-MM-dd hh:mm:ss' */
    /*@return 格式化日期时间描述 */
    dateFormat:function(date,format){
        if(!format || typeof format !== 'string'){
            console.error('format is undefiend or type is Error');
            return '';
        }
        date = date instanceof Date? date : (typeof date === 'number'|| typeof date === 'string')? new Date(date): new Date();
        //解析
        let formatReg = {
            'y+': date.getFullYear(),
            'M+': date.getMonth()+1,
            'd+': date.getDate(),
            'h+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds()
        }
        for(let reg in formatReg){
            if(new RegExp(reg).test(format)){
                let match = RegExp.lastMatch;
                format = format.replace(match, formatReg[reg]< 10 ? '0'+formatReg[reg]: formatReg[reg].toString() );
            }
        }
        return format;
    },

    /*@param date 时间戳*/
    /*@return 语义化的时间描述：'n月前'/'n周前'/'n天前'/'n小时前'/'n分钟前'/'刚刚' */
    formatMsgTime:function(date){  //格式化时间戳
        let result;
        let timePublish = new Date(date);
        let timeNow = Math.round(new Date().getTime()/1000);
        let minute = 1 * 60;
        let hour = minute * 60;
        let day = hour * 24;
        let month = day * 30;
        let diffValue = timeNow - data;
        let diffMonth = diffValue / month;
        let diffWeek = diffValue / (7 * day);
        let diffDay = diffValue / day;
        let diffHour = diffValue / hour;
        let diffMinute = diffValue / minute;
        if (diffMonth > 3) {
            result = timePublish.getFullYear() + "-";
            result += timePublish.getMonth() + "-";
            result += timePublish.getDate();
        }
        else if (diffMonth > 1) {
            result = parseInt(diffMonth) + "月前";
        }
        else if (diffWeek > 1) {
            result = parseInt(diffWeek) + "周前";
        }
        else if (diffDay > 1) {
            result = parseInt(diffDay) + "天前";
        }
        else if (diffHour > 1) {
            result = parseInt(diffHour) + "小时前";
        }
        else if (diffMinute > 1) {
            result = parseInt(diffMinute) + "分钟前";
        }
        else {
            result = "刚刚";
        }
        return result;
    },
    /**************************  日期时间类型工具函数  **************************/

    /**************************  变量类型检测  **************************/
    isNumber: function(value) {
        return Object.prototype.toString.call(value) == '[object Number]'
    },
    isString: function(value) {
        return Object.prototype.toString.call(value) == '[object String]'
    },
    isArray: function(value) {
        return Object.prototype.toString.call(value) == '[object Array]'
    },
    isBoolean: function(value) {
        return Object.prototype.toString.call(value) == '[object Boolean]'
    },
    isUndefined:function (value) {
        return value === undefined
    },
    isNull: function (value) {
        return value === null
    },
    isSymbol: function (value) {
        return Object.prototype.toString.call(value) == '[object Symbol]'
    },
    isObject: function (value) {
        return ( Object.prototype.toString.call(value) == '[object Object]'
            ||
            // if it isn't a primitive value, then it is a common object
            (
                !isNumber(value) &&
                !isString(value) &&
                !isBoolean(value) &&
                !isArray(value) &&
                !isNull(value) &&
                !isFunction(value) &&
                !isUndefined(value) &&
                !isSymbol(value)
            )
        )
    },
    //是否是一个空对象
    isEmptyObject: function(obj) {
        if(!this.isObject(obj)) {
            return false
        }
        for (let key in obj) {
            return false
        }
        return true
    },
    //是否是一个空数组
    isEmptyArray: function (array) {
        if(!isArray(array)) {
            return false
        }
        return array.length > 0 ? false : true
    },

}
module.exports = Tools;
